﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DB2Provider;
using System.Data;

namespace Bussiness
{
    public class DB2Time
    {
        /// <summary>
        /// 获取DB2服务器日期
        /// 默认格式：2010-6-6或2010-11-11
        /// </summary>
        /// <returns></returns>
        public string GetDBDate()
        {            
            string reDate = "";
            DataLink link = new DataLink();
            string sql = "SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reDate = dt.Rows[0][0].ToString();
            reDate = reDate.Substring(0, reDate.Length - 8);
            return reDate;
        }
        /// <summary>
        /// 获取DB2服务器年份
        /// </summary>
        /// <returns></returns>
        public int GetDBYear()
        {
            int reYear=0;
            DataLink link = new DataLink();
            string sql = "SELECT YEAR(CURRENT DATE) FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reYear = Convert.ToInt32(dt.Rows[0][0]);
            return reYear;
        }
        /// <summary>
        /// 返回DB2服务器月份
        /// </summary>
        /// <returns></returns>
        public int GetDBMonth()
        {
            int reMonth = 0;
            DataLink link = new DataLink();
            string sql = "SELECT MONTH(CURRENT DATE) FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reMonth = Convert.ToInt32(dt.Rows[0][0]);
            return reMonth;
        }
        /// <summary>
        /// 返回DB2服务器日期
        /// </summary>
        /// <returns></returns>
        public int GetDBDay()
        {
            int reDay = 0;
            DataLink link = new DataLink();
            string sql = "SELECT DAY(CURRENT DATE) FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reDay = Convert.ToInt32(dt.Rows[0][0]);
            return reDay;
        }
        /// <summary>
        /// 获取DB2服务器时间
        /// 默认格式：9:08:20
        /// </summary>
        /// <returns></returns>
        public System.TimeSpan GetDBTime()
        {

            System.TimeSpan reTime = new TimeSpan();
            DataLink link = new DataLink();
            string sql = "SELECT CURRENT TIME FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reTime = (System.TimeSpan)dt.Rows[0][0];
            return reTime ;
        }
        /// <summary>
        /// 返回当前时
        /// </summary>
        /// <returns></returns>
        public int GetTimeHours()
        {
            int reHours = 0;
            DataLink link = new DataLink();
            string sql = "SELECT HOUR(CURRENT TIME) FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reHours = Convert.ToInt32(dt.Rows[0][0]);
            return reHours;
        }
        /// <summary>
        /// 返回当前分
        /// </summary>
        /// <returns></returns>
        public int GetTimeMinute()
        {
            int reMinute = 0;
            DataLink link = new DataLink();
            string sql = "SELECT MINUTE(CURRENT TIME) FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reMinute = Convert.ToInt32(dt.Rows[0][0]);
            return reMinute;
        }
        /// <summary>
        /// 返回当前秒
        /// </summary>
        /// <returns></returns>
        public int GetTimeSecond()
        {
            int reSecond = 0;
            DataLink link = new DataLink();
            string sql = "SELECT SECOND(CURRENT TIME) FROM SYSIBM.SYSDUMMY1";
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
                reSecond = Convert.ToInt32(dt.Rows[0][0]);
            return reSecond;
        }
        /// <summary>
        /// 返回时间截格式
        /// </summary>
        /// <param name="ts">时间截</param>
        //  <returns></returns>
        public string FormatDiffTime(TimeSpan ts)
        {
            string diffTime = "";
            if (ts.Days != 0)
                diffTime += ts.Days + "天";
            if (ts.Hours != 0)
                diffTime += ts.Hours + "小时";
            if (ts.Minutes != 0)
                diffTime += ts.Minutes + "分";
            if (ts.Seconds != 0)
                diffTime += ts.Seconds + "秒";
            return diffTime;
        }
        /// <summary>
        /// 时间格式
        /// </summary>
        /// <param name="endTime"></param>
        /// <param name="startTime"></param>
        /// <returns></returns>
        public string FormatDiffTime(DateTime endTime, DateTime startTime)
        {
            string diffTime="";
            TimeSpan ts = endTime.Subtract(startTime);
            diffTime = FormatDiffTime(ts);
            return diffTime;
        }
        /// <summary>
        /// 返回上周日期
        /// </summary>
        public DateTime GetPreviewWeek()
        {
            TimeSpan ts=new TimeSpan(7,0,0,0);
            DateTime preWeek = DateTime.Now.Subtract(ts);
            return preWeek;
            
        }
        /// <summary>
        /// 日期比较
        /// </summary>
        /// <param name="dt1"></param>
        /// <param name="dt2"></param>
        /// <returns></returns>
        public int CompareDateTime(DateTime dt1,DateTime dt2)
        {
            return dt1.CompareTo(dt2);
        }
    }
}
